/**
 * 
 * 广州锐鹏计算机科技有限公司（www.ruipengkj.com）
 * 
 * 
 * Copyright (c) 2011-2012 RUIPENGKJ.Co.Ltd. All rights reserved.
 * 
 * */
package com.ruipengkj.view;

import java.awt.Component;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;

import javax.swing.JTextArea;
/**
 * 说明：
 * 移动组建事件
 * @Author：黄盛强
 * @Date：2012-8-9
 */
public class MoveListener implements MouseListener, MouseMotionListener {

//	private Cursor cursor = new Cursor(Cursor.MOVE_CURSOR);//移动光标
	private Cursor cursor_w = new Cursor(Cursor.W_RESIZE_CURSOR);//左、右拉伸光标
	private Cursor cursor_s = new Cursor(Cursor.S_RESIZE_CURSOR);//下方拉伸光标
	private Cursor cursor_se = new Cursor(Cursor.SE_RESIZE_CURSOR);//右下角拉伸光标
	
//	private Component comp;
	
	//鼠标拖动时
	@Override
	public void mouseDragged(MouseEvent e) {
		Component comp = (Component)e.getSource();
		
		if (comp instanceof JTextArea) {
			if (comp.getCursor() == cursor_w) {
				if (e.getX() <= 25)
					return;
				Dimension dimension = new Dimension(e.getX()+2, comp.getHeight());
				comp.setSize(dimension);
				return;
			} else if (comp.getCursor() == cursor_s) {
				if (e.getY() <= 25)
					return;
				Dimension dimension = new Dimension(comp.getWidth(), e.getY()+2);
				comp.setSize(dimension);
				return;
			} else if (comp.getCursor() == cursor_se) {
				int height = e.getY()+2;
				int width = e.getX()+2;
				if (height <= 25)
					height = 25;
				if (width <= 25)
					width = 25;
				Dimension dimension = new Dimension(width, height);
				comp.setSize(dimension);
				return;
			}
		}
		int x = comp.getX() + e.getX()- (int)p.getX();
		int y = comp.getY() + e.getY() - (int)p.getY();
		comp.setLocation(x, y);
	}
	
	//鼠标在组件上移动时
	@Override
	public void mouseMoved(MouseEvent e) {
//		Component comp = (Component)e.getSource();
//		
//		if (comp instanceof JTextArea) {
//			if (e.getX() == comp.getWidth() - 1 || e.getX() == comp.getWidth() - 2) {
//				if (e.getY() == comp.getHeight() - 1 || e.getY() == comp.getHeight() - 2)
//					comp.setCursor(cursor_se);
//				else
//					comp.setCursor(cursor_w);
//			} else if (e.getY() == comp.getHeight() - 1 || e.getY() == comp.getHeight() - 2) {
//				comp.setCursor(cursor_s);
//			} else if (e.getX() < comp.getWidth() - 3 && ((Component)e.getSource()).getCursor() == cursor_w) {
//				comp.setCursor(cursor);
//			} else if (e.getY() < comp.getHeight() - 3 && ((Component)e.getSource()).getCursor() == cursor_s) {
//				comp.setCursor(cursor);
//			} else if (e.getX() < comp.getWidth() - 3 && e.getY() < comp.getHeight() - 3 && ((Component)e.getSource()).getCursor() == cursor_se) {
//				comp.setCursor(cursor);
//			}
//		} else {
//			comp.setCursor(cursor);
//		}
	}

	//双击时，可编辑文本，单击却删除框给选中时，激活删除
	@Override
	public void mouseClicked(MouseEvent e) {
	}

	//按下组件时 获取原始坐标
	Point p = new Point(0, 0);
	@Override
	public void mousePressed(MouseEvent e) {
		p = e.getPoint();
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

}
